package com.wing.res.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wing.common.exception.BusinessException;
import com.wing.common.utils.JsonResult;
import com.wing.common.utils.PageResult;
import com.wing.res.model.entity.ResProductQualityTemplate;
import com.wing.res.model.response.ResProductQualityTemplateSearchVo;
import com.wing.res.service.ResBenchmarkBookQualityService;
import com.wing.res.service.ResProductQualityTemplateService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;


/**
 * 质检模板-Controller
 *
 * @Author xiayang
 **/
@Slf4j
@Validated
@Api(value = "质检模板管理", tags = "#/res/quality/qualityTemplate")
@RestController("resProQualityTemplateController")
@RequestMapping("quality/admin/qualityTemplate")
public class ResProQualityTemplateController {

    @Resource
    private ResProductQualityTemplateService resProductQualityTemplateService;

    @Resource
    private ResBenchmarkBookQualityService resBenchmarkBookQualityService;

    @ApiOperation(value = "查询质检模板", notes = "搜索-模板")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "第几页", defaultValue = "1", example = "1", dataType = "Integer", paramType = "query")
            ,@ApiImplicitParam(name = "limit", value = "每页多少条", defaultValue = "10", example = "10", dataType = "Integer", paramType = "query")
            ,@ApiImplicitParam(name = "sort", value="排序字段名", defaultValue = "create_time", example = "create_time", dataType = "String", paramType = "query")
            ,@ApiImplicitParam(name = "order", value="排序规则(asc:正序, desc:倒序)", defaultValue = "asc", example = "asc", dataType = "String", paramType = "query")
    })
    @GetMapping("/list")
    public PageResult<ResProductQualityTemplate> list(
            @RequestParam(defaultValue = "1") Integer page
            ,@RequestParam(defaultValue = "10") Integer limit
            ,@RequestParam(defaultValue = "create_time") String sort
            ,@RequestParam(defaultValue = "asc") String order
            ,Long materialTypeId
            ,Long qualityTestingTypeId
            ,Long productNatureId
            ,String name
    ) {
        Page<ResProductQualityTemplate> pg = new Page<>(page, limit);
        Map<String,Object> map = new HashMap<>();
        map.put("materialTypeId", materialTypeId);
        map.put("qualityTestingTypeId", qualityTestingTypeId);
        map.put("productNatureId", productNatureId);
        map.put("name", name);
        Page<ResProductQualityTemplate> list = resProductQualityTemplateService.pageList4app(pg, map);
        return new PageResult<>(list.getRecords(), list.getTotal());
    }

    @ApiOperation(value = "添加质检模板", notes = "添加")
    @PostMapping("/add")
    public JsonResult add(@RequestBody ResProductQualityTemplateSearchVo template) {
        try {
            resProductQualityTemplateService.addTemplate(template);
        } catch (BusinessException e) {
            return JsonResult.fail(e.getMessage());
        }
        return JsonResult.success("添加成功");
    }

    @ApiOperation(value = "更新质检模板", notes = "更新")
    @PostMapping("/update")
    public JsonResult upadte(@RequestBody  ResProductQualityTemplateSearchVo template) {
        if(resProductQualityTemplateService.updateTemplate(template)>0) {
            return JsonResult.success("更新成功");
        }else {
            return JsonResult.fail("更新失败");
        }
    }


    @ApiOperation(value = "查询详情", notes = "详情")
    @GetMapping("/detail")
    public JsonResult detail(Long templateId) {
        return JsonResult.success(resProductQualityTemplateService.getDetailBy(templateId));
    }

    /**
     * 根据基准书id查询 质检详情
     * @param bookId
     * @return
     */
    @ApiOperation(value = "基准书质检-列表", notes = "基准书质检-列表")
    @GetMapping("/getQualityInfoBY/{bookId}")
    public JsonResult getQualityInfoBY(@PathVariable("bookId") Long bookId) {
        return JsonResult.success(resBenchmarkBookQualityService.getQualityInfoBY(bookId));
    }




    @ApiOperation(value = "模板作废/恢复", notes = "废除/恢复")
    @PutMapping("/disableOrOpen/{templateId}/{status}")
    public JsonResult disableOrOpen(@PathVariable Long templateId,@PathVariable String status) {

        if(resProductQualityTemplateService.disableOrOpen(templateId,status)>0) {
            return JsonResult.success("操作成功");
        }else {
            return JsonResult.fail("操作失败");
        }
    }

}
